import { init, getInstance } from 'ts-indexdb';

// 封装chrome通知方法
function createNotification(data:Record<string,string>) {
  let notificationId:string|null=null
  chrome.notifications.clear('storageNotication')
  chrome.notifications.create(
    'storageNotication',
    {
    type: "basic",
    title: data.title,
    message: data.message,
    iconUrl: "./src/assets/icons/icon48.png",
    },
    (id) => {
      console.log('notifications created success:', id)
      notificationId = id
    }
  )
  return notificationId
}

// 链接数据库，返回库，表
async function initDB(dbName:string = 'Douyin_DataBase',tableName:string = 'chart_message') {
  await init({
    dbName: dbName,        // 数据库名称               
    version: 1,             // 版本号                
    tables: [                               
      {
        tableName:tableName,         // 表名         
        option: { keyPath: "id", autoIncrement: true }, // 指明主键为id
        indexs: [    // 数据库索引
          {
            key: "name",
            option: {
              unique: false
            }
          },
          {
            key: "comment",
            option: {
              unique:false    
            }
          },
          {
            key: "gift",
            option: {
              unique:false    
            }
          }
        ]
      }
    ]
  })
  return getInstance
}
export {
  createNotification,
  initDB
}